using UnityEngine;
using System.Collections;

public class MovementHandler : MonoBehaviour
{
    #region Attributs
    private Vector2 touchDelta;
    private string direction;
    private Const.ACdirection previousDirection;
    private bool wasStationary;

    public int SwiperTweaker = 5;
    #endregion

    // Use this for initialization
	void Start ()
    {
        #if UNITY_ANDROID
        Input.multiTouchEnabled = true;
        #endif
        wasStationary = false;
    }

	// Update is called once per frame
	void Update () 
    {
        var archeoCompo = this.GetComponent<Archeologist>();
        if (null == archeoCompo)
            return;

        #if UNITY_ANDROID
        if (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Began) // Finger Start touch
            previousDirection = archeoCompo.CurrentDir;

        //if (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Stationary) // Finger don't move
        //{
        //    wasStationary = true;
        //    archeoCompo.CurrentDir = Const.ACdirection.STOP;
        //}
        //else 
        if (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Moved && !wasStationary) // Finger Swipe
        {
            direction = string.Empty;
            touchDelta = Input.GetTouch(0).deltaPosition;
            int x = (int)touchDelta.x;
            int y = (int)touchDelta.y;
            
            if(Mathf.Abs(x) > Mathf.Abs(y)) // RIGHT & LEFT
            {
                if(x > 0)
                {
                    direction = "RIGHT";
                    archeoCompo.CurrentDir = Const.ACdirection.RIGHT;
                }
                else
                {
                    direction = "LEFT";
                    archeoCompo.CurrentDir = Const.ACdirection.LEFT;
                }
            }
            else// UP & DOWN
            {
                if(y > 0)
                {
                    direction = "UP";
                    archeoCompo.CurrentDir = Const.ACdirection.UP;
                }
                else
                {
                    direction = "DOWN";
                    archeoCompo.CurrentDir = Const.ACdirection.DOWN;
                }
            }
            if (y < x)
            {
                if (x > 0 && y < 0 && y < -SwiperTweaker)
                {
                    if(Mathf.Abs(x) > Mathf.Abs(y)) // Swipe RIGHT
                    {
                        direction = "RIGHT";
                        archeoCompo.CurrentDir = Const.ACdirection.RIGHT;
                    }
                    else // Swipe DOWN
                    {
                        direction = "DOWN";
                        archeoCompo.CurrentDir = Const.ACdirection.DOWN;
                    }
                }
                else if(x > 0 && y > 0 && x < -SwiperTweaker)// Swipe Right
                {
                    direction = "RIGHT";
                    archeoCompo.CurrentDir = Const.ACdirection.RIGHT;
                }

            }

            direction = string.Concat(direction, touchDelta.ToString());
        }
        //else if (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Ended
        //    && Const.ACdirection.STOP == archeoCompo.CurrentDir) // Finger release
        //{
        //    if (wasStationary)
        //        wasStationary = false;
        //    archeoCompo.CurrentDir = previousDirection;
        //}

        #else
        if (Input.GetKey(KeyCode.Z) || Input.GetKey(KeyCode.UpArrow))
            archeoCompo.CurrentDir = Const.ACdirection.UP;
        if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
            archeoCompo.CurrentDir = Const.ACdirection.DOWN;
        if (Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.LeftArrow))
            archeoCompo.CurrentDir = Const.ACdirection.LEFT;
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
            archeoCompo.CurrentDir = Const.ACdirection.RIGHT;
        #endif
    }
}
